package cn.mdmm.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.mdmm.entity.user.WxUserDetailedInfoModel;
import cn.mdmm.service.memcached.MemcachedService;
import cn.mdmm.service.user.WxUserDetailedInfoService;
import cn.mdmm.service.weixin.WeiXinServer;
import cn.modoumama.common.utils.ConfigProperty;

/**
 * 类描述： 
 * 创建人：邓强   
 * 创建时间：2016年11月21日 上午8:50:20     
 * 修改人：  
 * 修改时间：
 * 修改备注：     
 * @version   V1.0      
 */
@Controller
@RequestMapping
public class IndexController {
	Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	MemcachedService memcachedService;
	@Autowired
	WxUserDetailedInfoService wxUserDetailedInfoService;
	@Autowired
	WeiXinServer WeiXinServer;
	
	@RequestMapping("/")
	public String toIndex(HttpServletRequest request){
		return "redirect:/home";
	}
	
	/**
	 * 活动页入口(授权)
	 * @return
	 */
	@RequestMapping("auth")
	public String toActivity(HttpServletRequest request) {
		if (StringUtils.isNotBlank(request.getParameter("state"))&&StringUtils.isNotBlank(request.getParameter("code"))) {
			String state=request.getParameter("state");   //uuid获取用户访问的地址
			String code=request.getParameter("code");	//微信授权码

			String url=(String) memcachedService.get(state);
			WxUserDetailedInfoModel  wxUserDetailedInfoModel = wxUserDetailedInfoService.getWxUserDetailedInfoByWeixin(ConfigProperty.getProperty("com.qq.weixin.accountNo"), code);
			request.getSession().setAttribute("wxuser", wxUserDetailedInfoModel);
			return "redirect:"+url;
		}else {
			return "redirect:/home";
		}
	}
}
